using System;
using System.Collections.Generic;
using System.Text;
using System.Xml;
using MCMLXVII.BackEnd.Services.OrganizationChart.Messages;
using MCMLXVII.BackEnd.Data.OrganizationChart;
using MCMLXVII.BackEnd.Services.Common.Messages;
using MCMLXVII.BackEnd.Pipeline;
using MCMLXVII.BackEnd.Data.SystemDatabase;
using MCMLXVII.Core.Common.BackEnd;
using MCMLXVII.BackEnd.Data.OrganizationChart.DataSets;
using MCMLXVII.BackEnd.Data.SystemDatabase.DataSets;
using MCMLXVII.BackEnd.Services.OrganizationChart.Messages.AdvancedFind;
using MCMLXVII.Core.Common;
//using Messages.DistributionLists;

namespace MCMLXVII.BackEnd.Services.OrganizationChart.BusinessActions
{
    public class DistributionLists
    {

        #region Constructors and Interfaces
        private CallContext _Context;
        public DistributionLists()
        {

        }

        public void Initialize(CallContext Context)
        {
            _Context = Context;
        }

        #endregion

        /*        public msgNewOrganizationTreeItemResponse CreateNewDistributionList(msgGenericItemRequest<string> req)
        {
            int RequestingMemberID = Convert.ToInt32(_Context.GetParameter("MemberID"));
            MCMLXVII.BackEnd.Data.OrganizationChart.DataSets.DSOrganizationTreeNewItem.NewCreatedItemRow row = DALDistributionLists.CreateNewDistributionList(req.Item, RequestingMemberID);
            if (row == null)
                return null;
            else
                return new msgNewOrganizationTreeItemResponse(Convert.ToInt32(row.Identity) , row.UpdateSequence);
        }


        public msgDistributionListInfo GetDistributionListInfo(msgDistributionListRequest req)
        {
            int RequestingMemberID = Convert.ToInt32(_Context.GetParameter("MemberID"));
            MCMLXVII.BackEnd.Data.OrganizationChart.DataSets.DSDistributionLists ds = DALDistributionLists.GetDistributionList(req.DistributionListID, req.IncludeMembers );
            if ((ds == null) || (ds.Tables["DistributionLists"].Rows.Count == 0))
                return null;
            else
            {
                DSDistributionLists.DistributionListsRow dsr = (DSDistributionLists.DistributionListsRow)ds.Tables["DistributionLists"].Rows[0];
                DSDistributionLists.DistributionListsMembersDataTable dt = (DSDistributionLists.DistributionListsMembersDataTable)ds.Tables["DistributionListsMembers"];
                DistributionListMember[] mem = null;
                if (req.IncludeMembers)
                {
                    mem = new DistributionListMember[dt.Rows.Count];
                    int i = 0;
                    foreach (System.Data.DataRow r in dt.Rows)
                    {
                        DSDistributionLists.DistributionListsMembersRow row = (DSDistributionLists.DistributionListsMembersRow)r;
                        mem[i] = new DistributionListMember(row.MemberID, row.IncludeMode);
                        i++;                    
                    }
                }
                return new msgDistributionListInfo(dsr.DistributionListID, dsr.Name, dsr.UpdateSequence, mem);
            }
        }


        public msgDistributionListInfo[] GetDistributionListsInfo(msgVoidRequest req)
        {
            int RequestingMemberID = Convert.ToInt32(_Context.GetParameter("MemberID"));
            MCMLXVII.BackEnd.Data.OrganizationChart.DataSets.DSDistributionLists.DistributionListsDataTable dt = DALDistributionLists.GetDistributionLists();
            if (dt == null)
                return null;
            else
            {
                    msgDistributionListInfo[] res = new msgDistributionListInfo[dt.Rows.Count];
                    int i = 0;
                    foreach (System.Data.DataRow r in dt.Rows)
                    {
                        DSDistributionLists.DistributionListsRow row = (DSDistributionLists.DistributionListsRow)r;
                        res[i] = new msgDistributionListInfo(row.DistributionListID, row.Name, row.UpdateSequence, null);
                        i++;
                    }
                    return res;
            }
                
        }


        public bool DeleteDistributionList(msgGenericItemRequest<int> req)
        {
            int RequestingMemberID = Convert.ToInt32(_Context.GetParameter("MemberID"));
            return DALDistributionLists.DeleteDistributionList(req.Item, RequestingMemberID);
        }


        public int UpdateDistributionList(msgDistributionListInfo req)
        {
            int RequestingMemberID = Convert.ToInt32(_Context.GetParameter("MemberID"));
            int updseq = DALDistributionLists.UpdateDistributionList(req.DistributionListID, req.Name, req.UpdateSequence, RequestingMemberID);
            int[] Members;
            if (req.Members == null)
                req.Members = new DistributionListMember[0];
                
            Members = new int[req.Members.Length];

            bool[] Recursive;
            Recursive = new bool[req.Members.Length];

            int i = 0;
            if (req.Members != null)
                foreach(DistributionListMember dm in req.Members)
                {
                    Members[i] = req.Members[i].MemberID;
                    Recursive[i] = (req.Members[i].IncludeMode == "R");
                    i++;            
                }

            updseq = DALDistributionLists.UpdateDistributionListMembership(req.DistributionListID, Members, Recursive, updseq, RequestingMemberID);
            return updseq;

        }


        public int CreateDistributionListExpansion(msgVoidRequest req)
        {
            return DALDistributionLists.CreateExpansion();
        }

        public msgDistributionListSID[] GetDistributionListLogins(int[] req)
        {
            MCMLXVII.BackEnd.Data.OrganizationChart.DataSets.DSDistributionLists.DistributionListLoginsDataTable dt =  DALDistributionLists.GetDistributionListLogins(req);
            if (dt.Rows.Count  == 0)
            {
                return null;
            }
            else
            {
                msgDistributionListSID[] res = new msgDistributionListSID[dt.Rows.Count];
                int i = 0;
                foreach (System.Data.DataRow r in dt.Rows)
                {
                    MCMLXVII.BackEnd.Data.OrganizationChart.DataSets.DSDistributionLists.DistributionListLoginsRow row = (MCMLXVII.BackEnd.Data.OrganizationChart.DataSets.DSDistributionLists.DistributionListLoginsRow)r;
                    res[i] = new msgDistributionListSID(row.Login, "N");
                    i++;
                }
                return res;
            }        
        }


        public msgDistributionListSID[] GetDistributionListLoginsDelta(msgDistributionListDeltaRequest req)
        {
            MCMLXVII.BackEnd.Data.OrganizationChart.DataSets.DSDistributionLists.DistributionListLoginsDeltaDataTable dt = DALDistributionLists.GetDistributionListLoginsDelta(req.DistributionLists, req.LastExpansionID );
            if (dt.Rows.Count  == 0)
            {
                return null;
            }
            else
            {
                msgDistributionListSID[] res = new msgDistributionListSID[dt.Rows.Count];
                int i = 0;
                foreach (System.Data.DataRow r in dt.Rows)
                {
                    MCMLXVII.BackEnd.Data.OrganizationChart.DataSets.DSDistributionLists.DistributionListLoginsDeltaRow row = (MCMLXVII.BackEnd.Data.OrganizationChart.DataSets.DSDistributionLists.DistributionListLoginsDeltaRow)r;
                    res[i] = new msgDistributionListSID(row.Login, row.Added);
                    i++;
                }
                return res;
            }
        }


        public msgDLExpandedTree GetDistributionListExpandedContent(msgGenericItemRequest<int> req)
        {
            //int MemberID = Convert.ToInt32(_Context.GetParameter("MemberID"));
            //string UserLang = MultiLingualContents.GetMemberLanguage(MemberID);            
            DSDistributionLists.DistributionListExpandedContentDataTable dt = DALDistributionLists.GetDistributionListExpandedContents(req.Item);

            if (dt.Rows.Count == 0)
                return null;
            else
            {
                msgDLExpandedTree Root = new msgDLExpandedTree();
                foreach (DSDistributionLists.DistributionListExpandedContentRow row in dt.Rows)
                {
                    if (row.MemberID == 1)
                    {
                        Root.MemberID = 1;
                        Root.Name = row.Name;
                        Root.IsRecursive = (row.IncludeMode == "R");
                        Root.MemberClass = 0;
                        Root.IsNative = (row.Native == "Y");
                        Root.FatherNodeID = 0;
                    }
                    else
                    {
                        msgDLExpandedTree node = new msgDLExpandedTree();
                        node.MemberID = row.MemberID;
                        node.MemberClass = row.MemberClass;
                        node.Name = row.Name;
                        node.IsNative = (row.Native == "Y");
                        node.IsRecursive = (row.IncludeMode == "R");
                        node.FatherNodeID = row.FatherNodeID;
                        msgDLExpandedTree FatherNode = GetFatherNode(Root, node.FatherNodeID);
                        if (FatherNode.FirstChild == null)
                        {
                            FatherNode.FirstChild = node;
                        }
                        else
                        {
                            FatherNode = FatherNode.FirstChild;
                            while (FatherNode.NextBrother != null)
                            {
                                FatherNode = FatherNode.NextBrother;
                            }
                            FatherNode.NextBrother = node;
                        }
                    }
                }
                return Root;
            }
        }
        private msgDLExpandedTree GetFatherNode(msgDLExpandedTree Root, int NodeID)
        {
            if (Root == null)
            {
                return null;
            }
            else
            {
                if (Root.MemberID == NodeID)
                {
                    return Root;
                }
                else
                {
                    if (Root.FirstChild == null)
                    {
                        if (Root.NextBrother == null)
                        {
                            return null;
                        }
                        else
                        {
                            return GetFatherNode(Root.NextBrother, NodeID);
                        }
                    }
                    else
                    {
                        msgDLExpandedTree AuxNode = GetFatherNode(Root.FirstChild, NodeID);
                        if (AuxNode != null)
                        {
                            return AuxNode;
                        }
                        else
                        {
                            if (Root.NextBrother == null)
                            {
                                return null;
                            }
                            else
                            {
                                return GetFatherNode(Root.NextBrother, NodeID);
                            }
                        }
                    }
                }
            }
        }

        public int[] GetDistributionListsChanged(msgGenericItemRequest<int> req)
        {
            DSDistributionLists.DistributionsListListDataTable dt = DALDistributionLists.GetDistributionListsChanged( req.Item );
            int[] res = new int[dt.Rows.Count];
            int i = 0;
            foreach( DSDistributionLists.DistributionsListListRow row in dt.Rows)
            {
                res[i] = row.DistributionListID;
                i++;            
            }
            return res;
        }
        */
    }
  
}
